package com.ymatou.iapi.liveseller.testcase;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.ymatou.iapi.liveseller.parameter.LiveProductHitListBean;
import com.ymatou.iapi.liveseller.service.LiveProductHitListCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.model.Userinfo;
import com.ymttest.database.sqlwapper.JUserWapper;
/**
*直播间商品点击列表Test
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_LiveProductHitList{
	private static LiveProductHitListBean liveproducthitlistBean;
	private static LiveProductHitListCall liveproducthitlistCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("直播间商品点击列表Test");
	}

	@Before
	public void caseUp() {
		liveproducthitlistBean=new LiveProductHitListBean();
		liveproducthitlistCall=new LiveProductHitListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(liveproducthitlistCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_LiveProductHitList_001() {
		Logger.start(true,"正确数据，成功");
		try {
			List<Userinfo> list = new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(0);
			//SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			List<String> ProductIdList = new ArrayList<>();
			int count = 10;
			for (int i = 0; i < count; i++) {
				String pid =ProductManagerCallServiceV2.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
				Logger.comment("创建商品：" + pid);
				ProductIdList.add(pid);
				Thread.sleep(500);
			}
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), ProductIdList);
			Collections.shuffle(ProductIdList);
			LiveRoomCallService.SortLiveProduct(createlive.getId(), createlive.getUserId(), ProductIdList);
			
			liveproducthitlistBean.setLiveId(createlive.getId());
			liveproducthitlistBean.setUserId(createlive.getUserId());
			
			liveproducthitlistCall.setData(liveproducthitlistBean);
			liveproducthitlistCall.callService();
			Logger.verifyEquals("200", liveproducthitlistCall.getString("Code"), "验证code");
			JsonArray ProductList = liveproducthitlistCall.getJsonObject().getAsJsonObject("Data").getAsJsonArray("ProductList");
			
			for (JsonElement jsonElement : ProductList) {
				JsonObject obj = jsonElement.getAsJsonObject();
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_LiveProductHitList_002() {
		Logger.start(true,"userId为空，失败");
		try {
			List<Userinfo> list = new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(0);
			//SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			List<String> ProductIdList = new ArrayList<>();
			int count = 10;
			for (int i = 0; i < count; i++) {
				String pid =ProductManagerCallServiceV2.getNewProductId(uinfo.getLoginId(), uinfo.getUserId(), 10);
				Logger.comment("创建商品：" + pid);
				ProductIdList.add(pid);
			}
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), ProductIdList);
			Collections.shuffle(ProductIdList);
			LiveRoomCallService.SortLiveProduct(createlive.getId(), createlive.getUserId(), ProductIdList);
			
			liveproducthitlistBean.setLiveId(createlive.getId());
			liveproducthitlistBean.setUserId(0);
			 
			liveproducthitlistCall.setData(liveproducthitlistBean);
			liveproducthitlistCall.callService();			
			Logger.verifyEquals("400", liveproducthitlistCall.getString("Code"), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_LiveProductHitList_003() {
		Logger.start(true,"商品id为空，失败");
		try {
			liveproducthitlistBean.setLiveId(0);
			liveproducthitlistBean.setUserId(0);
			
			liveproducthitlistCall.setData(liveproducthitlistBean);
			liveproducthitlistCall.callService();			
			Logger.verifyEquals("400", liveproducthitlistCall.getString("Code"), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
}